package com.leyou.item.controller;

import com.leyou.common.pojo.PageResult;
import com.leyou.item.pojo.Brand;
import com.leyou.item.service.IBrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author CHERRY
 * @date 2018-09-02-10:57
 */
@Controller
@RequestMapping("brand")
public class BrandController {
    @Autowired
    private IBrandService brandService;

    /**
     * 根据条件分页查询品牌
     *
     * @param key
     * @param page
     * @param rows
     * @param sortBy
     * @param desc
     * @return
     */
    @GetMapping("page")
    public ResponseEntity<PageResult> queryBrandByPage(
            @RequestParam(value = "key", required = false) String key,
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "5") Integer rows,
            @RequestParam(value = "sortBy", required = false) String sortBy,
            @RequestParam(value = "desc", required = false) Boolean desc) {
        //service 分页查询品牌
        PageResult pageResult = this.brandService.queryBrandByPage(key, page, rows, sortBy, desc);
        //分页 查询品牌结果为空 404
        if (CollectionUtils.isEmpty(pageResult.getItems())) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 新增品牌
     *
     * @param cids
     * @param brand
     * @return
     */
    @PostMapping
    public ResponseEntity insertBrand(@RequestParam("cids") List<Long> cids, Brand brand) {

        this.brandService.insertBrand(cids, brand);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 编辑更新品牌信息
     *
     * @param cids
     * @param brand
     * @return
     */
    @PutMapping
    public ResponseEntity<Void> updateBrand(@RequestParam("cids") List<Long> cids, Brand brand) {
        try {
            this.brandService.updateBrand(brand, cids);
            //操作成功，未返回数据
            return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
        } catch (Exception e) {
            e.printStackTrace();
        }
        //异常，系统内部错误
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 根据bid删除品牌信息
     *
     * @param bid
     * @return
     */
    @DeleteMapping("{bid}")
    public ResponseEntity<Void> deleteBrandByBid(@PathVariable("bid") Long bid) {

        this.brandService.deleteBrandByBid(bid);
        //操作成功，未返回数据
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 根据三级类目查询品牌集合
     *
     * @param cid
     * @return
     */
    @GetMapping("cid/{cid}")
    public ResponseEntity<List<Brand>> queryBrandByCid(@PathVariable("cid") Long cid) {
        List<Brand> brands = this.brandService.queryBrandByCid(cid);
        return ResponseEntity.ok(brands);
    }

    @GetMapping("bid")
    public ResponseEntity<Brand> queryBrandById(@RequestParam("id") Long id) {
        Brand brand = this.brandService.queryBrandByBid(id);
        if (brand == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(brand);
    }

    /**
     * 根据多个id查询品牌
     *
     * @return
     */
    @PostMapping("list")
    public ResponseEntity<List<Brand>> queryBrandByIds(@RequestParam("ids") List<Long> ids) {
        List<Brand> brands = this.brandService.queryBrandByIds(ids);
        if (CollectionUtils.isEmpty(brands)) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(brands);
    }
}
